/*
 * Copyright (C) 2012 lytsing.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.idreems.imagesns.adapters;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.androidquery.AQuery;
import com.idreems.imagesns.Consts;
import com.idreems.imagesns.R;
import com.idreems.imagesns.model.Statuses;
import com.idreems.imagesns.util.DateTimeUtils;
import com.idreems.imagesns.util.Log;
import com.idreems.imagesns.util.Util;
import com.weibo.sdk.android.WeiboException;

/**
 * Statuses item adapter
 */
public class StatusItemAdapter extends BaseAdapter {

    private Context mContext;

    private List<Statuses> mStatuses;

    public StatusItemAdapter(Context context) {
        mContext = context;
        mStatuses = new ArrayList<Statuses>();
    }

    @Override
    public int getCount() {
        return mStatuses.size();
    }

    @Override
    public Object getItem(int position) {
        return mStatuses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder {
        ImageView userImage;
        TextView userName;
        ImageView gps;
        ImageView pic;
        TextView time;
        TextView content;
        ImageView thumbnailPic;
        TextView tweetForm;
        ImageView tweetRedirectPic;
        TextView tweetRedirect;
        ImageView tweetCommentPic;
        TextView tweetComment;
        ImageView tweetAttitudePic;
        TextView tweetAttitude;
        TextView retweetedText;
        View subLayout;
        ImageView tweetUploadPic2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Statuses statuses = null;
        ViewHolder holder;

        // Log.d("getView:" + position);

        if (position < mStatuses.size()) {
            statuses = mStatuses.get(position);
        }

        if (convertView == null) {
            convertView = Util.inflateView(R.layout.list_item_status, mContext);
            holder = new ViewHolder();
            holder.userImage = (ImageView) convertView.findViewById(R.id.ivItemPortrait);
            holder.userName = (TextView) convertView.findViewById(R.id.tvItemName);
            holder.gps = (ImageView) convertView.findViewById(R.id.ivItemGps);
            holder.pic = (ImageView) convertView.findViewById(R.id.ivItemPic);
            holder.time = (TextView) convertView.findViewById(R.id.tvItemDate);
            holder.content = (TextView) convertView.findViewById(R.id.tvItemContent);
            holder.thumbnailPic = (ImageView) convertView.findViewById(R.id.tweet_upload_pic1);
            holder.tweetForm = (TextView) convertView.findViewById(R.id.tweet_form);
            holder.tweetRedirectPic = (ImageView) convertView.findViewById(R.id.tweet_redirect_pic);
            holder.tweetRedirect = (TextView) convertView.findViewById(R.id.tweet_redirect);
            holder.tweetCommentPic = (ImageView) convertView.findViewById(R.id.tweet_comment_pic);
            holder.tweetComment = (TextView) convertView.findViewById(R.id.tweet_comment);
            holder.tweetAttitudePic = (ImageView) convertView.findViewById(R.id.tweet_attitude_pic);
            holder.tweetAttitude = (TextView) convertView.findViewById(R.id.tweet_attitude);
            holder.subLayout = convertView.findViewById(R.id.subLayout);
            holder.retweetedText = (TextView) convertView.findViewById(R.id.tvItemSubContent);
            holder.tweetUploadPic2 = (ImageView) convertView.findViewById(R.id.tweet_upload_pic2);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        AQuery aq = new AQuery(convertView);

        // Enable hardware acceleration if the device has API 11 or above
        aq.hardwareAccelerated11();

        Bitmap placeholder = aq.getCachedImage(R.drawable.portrait_image_empty);

        if (statuses.user != null) {
            if (aq.shouldDelay(position, convertView, parent, statuses.user.profile_image_url)) {
                aq.id(holder.userImage).image(placeholder, 1.0f);
            } else {
                aq.id(holder.userImage).image(statuses.user.profile_image_url, true, true, 0, 0, placeholder, AQuery.FADE_IN);
            }
            // userID
            final long userID = statuses.user.id;
            aq.id(holder.userImage).clicked(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Consts.ACTION_SHOW_USER_TIMELINE);
                    intent.putExtra(Consts.USER_ID, userID);

                    mContext.startActivity(intent);
                }
            });

            holder.userName.setText(statuses.user.name);
        }

        holder.gps.setVisibility(statuses.geo != null ? View.VISIBLE : View.GONE);
        if (statuses.geo != null) {
            Log.d(statuses.geo.toString());
        }

        String time = "";

        try {
            Date date = Util.parseDate(statuses.created_at);
            if (date != null) {
                time = DateTimeUtils.getInstance(mContext).getTimeDiffString(date.getTime());
            }
        } catch (WeiboException e) {
            Log.e("StatusCode:" + e.getStatusCode() + " " + e.getMessage());
        }

        holder.time.setText(time);
        holder.content.setText(statuses.text, TextView.BufferType.SPANNABLE);
        Util.textHighlight(holder.content, "#", "#");
        // Util.textHighlight(holder.content, "@",":");
        Util.textHighlight(holder.content, "http://", " ");

        String source = statuses.source;
        if (statuses.geo != null) {
            String distanceString = String.format(Locale.ENGLISH, "%.1f", Util.distFrom(
                    Consts.COORDINATES_LAT, Consts.COORDINATES_LONG, statuses.geo.coordinates.get(0).floatValue(), statuses.geo.coordinates
                            .get(1).floatValue()));
            source += String.format(mContext.getResources().getString(R.string.distance), distanceString);
        }

        holder.tweetForm.setText(Html.fromHtml(String.format(mContext.getResources().getString(R.string.from), source)));

        holder.tweetRedirectPic.setVisibility((statuses.reposts_count > 0) ? View.VISIBLE : View.GONE);
        holder.tweetRedirect.setVisibility((statuses.reposts_count > 0) ? View.VISIBLE : View.GONE);
        if (statuses.reposts_count > 0) {
            aq.id(R.id.tweet_redirect_pic).visible();
            holder.tweetRedirect.setText(String.valueOf(statuses.reposts_count));
        }

        holder.tweetCommentPic.setVisibility((statuses.comments_count > 0) ? View.VISIBLE : View.GONE);
        holder.tweetComment.setVisibility((statuses.comments_count > 0) ? View.VISIBLE : View.GONE);
        if (statuses.comments_count > 0) {
            holder.tweetComment.setText(String.valueOf(statuses.comments_count));
        }

        holder.tweetAttitudePic.setVisibility((statuses.attitudes_count > 0) ? View.VISIBLE : View.GONE);
        holder.tweetAttitude.setVisibility((statuses.attitudes_count > 0) ? View.VISIBLE : View.GONE);
        if (statuses.attitudes_count > 0) {
            holder.tweetAttitude.setText(String.valueOf(statuses.attitudes_count));
        }

        if (Log.DEBUG) {
            Log.d("thumbnail_pic:" + statuses.thumbnail_pic);
        }

        holder.thumbnailPic.setVisibility((statuses.thumbnail_pic != null) ? View.VISIBLE : View.GONE);
        if (statuses.thumbnail_pic != null) {

            final String middleImageUrl = statuses.bmiddle_pic;
            final String originalPicUrl = statuses.original_pic;

            Bitmap loadingImg = aq.getCachedImage(R.drawable.chat_pic_loading);
            if (aq.shouldDelay(position, convertView, parent, statuses.thumbnail_pic)) {
                aq.id(holder.thumbnailPic).image(loadingImg, 1.0f).visible();
            } else {
                aq.id(holder.thumbnailPic).image(statuses.thumbnail_pic, false, true, 0, 0, loadingImg, AQuery.FADE_IN).visible();
            }

            aq.id(holder.thumbnailPic).clicked(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Consts.ACTION_SHOW_IMAGE_VIWVER);
                    intent.putExtra(Consts.MIDDLE_IMAGE_URL_KEY, middleImageUrl);
                    intent.putExtra(Consts.ORIGINAL_PIC_URL_KEY, originalPicUrl);

                    mContext.startActivity(intent);
                }

            });
        }

        holder.subLayout.setVisibility(statuses.retweeted_status != null ? View.VISIBLE : View.GONE);
        if (statuses.retweeted_status != null) {

            String text = "";
            if (statuses.retweeted_status.user != null) {
                text = "@" + statuses.retweeted_status.user.name + ":";
            }

            text += statuses.retweeted_status.text;
            ;

            holder.retweetedText.setText(text, TextView.BufferType.SPANNABLE);
            Util.textHighlight(holder.retweetedText, "#", "#");
            // Util.textHighlight(holder.retweetedText, "@",":");
            Util.textHighlight(holder.retweetedText, "http://", " ");

            if (statuses.retweeted_status.thumbnail_pic != null) {
                final String bmiddlePicUrl = statuses.retweeted_status.bmiddle_pic;
                final String originalPicUrl = statuses.retweeted_status.original_pic;

                aq.id(holder.tweetUploadPic2).image(statuses.retweeted_status.thumbnail_pic).visible().clicked(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Consts.ACTION_SHOW_IMAGE_VIWVER);
                        intent.putExtra(Consts.MIDDLE_IMAGE_URL_KEY, bmiddlePicUrl);
                        intent.putExtra(Consts.ORIGINAL_PIC_URL_KEY, originalPicUrl);
                        mContext.startActivity(intent);
                    }
                });
            }
        }

        return convertView;
    }

    public void addStatuses(List<Statuses> statuses) {
        mStatuses.addAll(statuses);
    }
    
    public void addStatuses(Statuses status) {
    	if (status!=null && mStatuses != null)
		{
    		mStatuses.add(status);
		}
    }

    public void addNewestStatuses(List<Statuses> statuses) {
        if (statuses == null || statuses.size() == 0) {
            return;
        }
        mStatuses.addAll(0, statuses);
    }
}
